package uestc.lj.gulishop.order.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import uestc.lj.gulishop.order.entity.OrderEntity;
import uestc.lj.gulishop.order.service.OrderService;

import java.io.IOException;

/**
 * @Author:Crazlee
 * @Date:2021/6/22
 */
@Component
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void test(OrderEntity orderEntity, Channel channel, Message message) throws IOException {

        System.out.println("收到过期的订单信息，准备关闭订单" + orderEntity.getOrderSn());
        try {
            // 解锁订单
            orderService.closeOrder(orderEntity);
            // 手动调用收单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 出现异常则重新入队
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
